<?php

namespace App\Http\Form;

use App\Http\Form\TikTokBuildShop\KfCashRegisterFormValidate;
use App\Http\Validate\OpenShop\InvestmentPromotion\UserIdentityCardValidate;
use App\Models\WorkOrder\WorkOrderKfCashRegisterModel;
use App\Support\CustomForm\Validate\ValidateInterface;
use App\Support\CustomForm\Validate\ValidateSupport;
use Illuminate\Support\Facades\Validator;

class FormValidate extends ValidateSupport implements ValidateInterface
{

    const DATE_VALIDATE_FORMAT_1 = "Y-m-d";
    const DATE_VALIDATE_FORMAT_2 = "y-m";
    const DATE_VALIDATE_FORMAT_3 = "m-d";
    const DATE_VALIDATE_FORMAT_4 = "y-m-d H:i:s";
    const DATE_VALIDATE_FORMAT_5 = "H:i";

    /**
     * 图片校验
     *
     * @param bool $required
     * @param int $min
     * @param int $max
     * @return ValidateSupport
     */
    public static function imgGroupValidate(bool $required = true, int $min = 1, int $max = 1): ValidateSupport
    {
        return static::getInstance()
                     ->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_ARRAY)
                     ->setContentLength($min, $max);
    }

    /**
     * 视频校验
     *
     * @param bool $required
     * @param int $min
     * @param int $max
     * @return ValidateSupport
     */
    public static function videoGroupValidate(bool $required = true, int $min = 1, int $max = 1): ValidateSupport
    {
        return static::getInstance()
                     ->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_ARRAY)
                     ->setContentLength($min, $max);
    }

    /**
     * @param string $provinceCodeField
     * @param string $cityCodeField
     * @param string $districtCodeField
     * @param string $addressPrefix
     * @param bool $required
     * @return ValidateSupport
     */
    public static function addressValidate(
        string $provinceCodeField = "provinceCode",
        string $cityCodeField = "cityCode",
        string $districtCodeField = "districtCode",
        string $addressPrefix = "addressPrefix",
        bool $required = true
    ): ValidateSupport {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_ARRAY)
                     ->setClosure(
                         function ($attribute, $value, $fail) use (
                             $provinceCodeField,
                             $cityCodeField,
                             $districtCodeField,
                             $addressPrefix
                         ) {
                             $validator = Validator::make(
                                 $value,
                                 [
                                     $provinceCodeField => "required|string|min:0|max:12",
                                     $cityCodeField     => "required|string|min:0|max:12",
                                     $districtCodeField => "required|string|min:0|max:12",
                                     $addressPrefix     => "required|string|min:0|max:120"
                                 ],
                                 [
                                     "string" => ":attribute 数据类型需为字符串",
                                     "int"    => ":attribute 数据类型需为字符串",
                                     "array"  => ":attribute 数据类型需为数组"
                                 ],
                                 [
                                     "provinceCode" => "省份code",
                                     "cityCode"     => "市级code",
                                     "areaCode"     => "区域code"
                                 ]
                             );
                             if ($validator->fails()) {
                                 $fail($validator->errors()->first());
                             }
                         }
                     );
    }

    /**
     * 手机号校验
     *
     * @param bool $required
     * @return FormValidate|ValidateSupport
     */
    public static function mobileValidate(bool $required = true)
    {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_STRING)
                     ->setContentLength(11, 11)
                     ->setClosure(
                         function ($attribute, $value, $fail) {
                             if (!preg_match('/^1\d{10}$/', $value)) {
                                 $fail("门店手机号格式错误!");
                             }
                         }
                     );
    }


    /**
     *
     * @param string $format
     * @param bool $required
     * @return FormValidate|ValidateSupport
     */
    public static function dateRangeValidate(string $format, bool $required = true)
    {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_ARRAY)
                     ->setContentLength(2, 2)
                     ->setClosure(
                         function ($attribute, $value, $fail) use ($format) {
                             $validator = Validator::make(
                                 $value,
                                 [
                                     "startTime" => "required|string|date_format:{$format}",
                                     "endTime"   => "required|string|date_format:{$format}",
                                 ]
                             );
                             if ($validator->fails()) {
                                 $fail($validator->errors()->first());
                             }
                         }
                     );
    }


    /**
     * 字符串校验
     *
     * @param int $min
     * @param int $max
     * @param bool $required
     * @return FormValidate|ValidateSupport
     */
    public static function stringValidate(int $min, int $max, bool $required = true)
    {
        return static::getInstance()
                     ->setRequired($required)
                     ->setDataType(parent::DATA_TYPE_STRING)
                     ->setContentLength($min, $max);
    }


    /**
     * @param bool $required
     * @return ValidateSupport
     */
    public static function identityCardValidate(bool $required = true): ValidateSupport
    {
        return static::getInstance()
                     ->setRequired($required)
                     ->setClosure(
                         function ($attribute, $value, $fail) {
                             if (!UserIdentityCardValidate::isValid($value)) {
                                 $fail("身份证号码不合法!");
                             }
                         }
                     );
    }

    /**
     * 时间规则校验
     *
     * @param string $dateRule
     * @param bool $required
     * @return FormValidate|ValidateSupport
     */
    public static function dateValidate(string $dateRule, bool $required = true)
    {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_STRING)
                     ->setDateRule($dateRule);
    }

    /**
     * @param array $enums
     * @param bool $required
     * @return FormValidate|ValidateSupport
     */
    public static function inIntValidate(array $enums, bool $required = true)
    {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_INT)
                     ->setIn(array_keys($enums));
    }
}
